package com.sunny.core.controller;

import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunny.core.bean.PersonInfo;
import com.sunny.core.bean.PublicDict;
import com.sunny.core.bean.UserInfo;
import com.sunny.core.service.IPublicDictService;
import com.sunny.core.utils.CommonResult;
import com.sunny.core.utils.CommonStringUtils;
import com.sunny.core.utils.Constant;

@Controller
@RequestMapping("/publicDict")
public class PublicDictController {

	@Resource(name = "publicDictService")
	private IPublicDictService publicDictService;

	/**
	 * 查询主字典
	 */
	@RequestMapping(value = "/query")
	public ModelAndView queryAll(HttpServletRequest request, HttpServletResponse response, Model m) throws Exception {


		//主字典列表
		List<PublicDict> publicDictMasterList = publicDictService.findMasterDictByType(null);
		//子字典列表
		List<PublicDict> publicDictList = null;
		
		
		PageInfo<PublicDict> pagehelper = null;
		int num = 1;
		int size = 10;
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");		
		String searchType = request.getParameter("searchType");
		
		if (!CollectionUtils.isEmpty(publicDictMasterList)){
			PublicDict publicDictMaster = publicDictMasterList.get(0);
			
			if (searchType == null) {
				searchType = publicDictMasterList.get(0).getDictType();
			}
			
			if (publicDictMaster != null) {

				publicDictList = publicDictService.findByType(searchType);
				
				if (pageNum != null && !"".equals(pageNum)) {
					num = Integer.parseInt(pageNum);
				}
				if (pageSize != null && !"".equals(pageSize)) {
					size = Integer.parseInt(pageSize);
				}
				String sortString = "dict_name.desc";
				Order.formString(sortString);

				PageHelper.startPage(num, size);

//				if (StringUtils.isNotBlank(searchType)) {
//					if (searchType.equals("policyCode")) {
//						policyInfo.setPolicyCode(searchValue);
//					}
//				}

				pagehelper = new PageInfo<PublicDict>(publicDictList);
			}
			
		}
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("pagehelper", pagehelper);

		m.addAttribute("publicDictMasterList", publicDictMasterList);
		m.addAttribute("publicDictList", publicDictList);
		m.addAttribute("total", pagehelper.getTotal());
		m.addAttribute("pageSize", pagehelper.getPageSize());
		m.addAttribute("pageNum", num);
		m.addAttribute("searchType", searchType);

		modelAndView.setViewName("public_dict/public_dict_list");
		return modelAndView;

	}
	
	/**
	 * 跳到保单详细信息新增页面
	 */
	@RequestMapping(value = "/toAdd", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model m) 
			throws Exception {

		ModelAndView modelAndView = new ModelAndView();
		
		String dictType = request.getParameter("dictType");
		
		PublicDict publicDict = publicDictService.findMasterDictByType(dictType).get(0);
		
		session.setAttribute("publicDict", publicDict);
		
		modelAndView.setViewName("public_dict/public_dict_crud");
		return modelAndView;
		
	}	
	
	
	@RequestMapping(value = "/add", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public CommonResult add(HttpServletRequest request,HttpSession session,PublicDict publicDict) throws Exception {	
		CommonResult commonResult = new CommonResult();
		
		UserInfo currentUser = (UserInfo) session.getAttribute("currentUser");
		publicDict.setParentId(publicDict.getDictId());
		publicDict.setDictId(CommonStringUtils.get32UUID());
		publicDict.setDelFlag(new Integer(0));
		publicDict.setCreateTime(new Date());
		publicDict.setCreateUserid(currentUser.getUserPid());
		publicDict.setCreateUsername(currentUser.getRealName());
		
		int result = publicDictService.save(publicDict);
		if (result == 1) {
			commonResult.setStatus(Constant.RESULT_SCUSSES_CODE);
			commonResult.setMsg(Constant.RESULT_ADDSUC_DESC);
		} else {
			commonResult.setStatus(Constant.RESULT_ERROR_CODE);
			commonResult.setMsg(Constant.RESULT_ADDERR_DESC);
		}
		return commonResult;
	}	
	
}
